package com.dense.kuiniu.floatcabinet.mvp.model;

import com.dense.kuiniu.floatcabinet.mvp.contract.QueryContract;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Book;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import org.litepal.LitePal;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * @author Administrator
 * @date 2020/11/2
 */
public class QueryModel extends BaseModel implements QueryContract.Model {
    public QueryModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public Observable<List<Book>> query(String key) {
        return Observable.create(new ObservableOnSubscribe<List<Book>>() {
            @Override
            public void subscribe(ObservableEmitter<List<Book>> emitter) throws Exception {
                List<Book> list;
                if (key.isEmpty()) {
                    list = LitePal.findAll(Book.class);
                } else {
                    list = LitePal.where("bookname like ?", "%" + key + "%").find(Book.class);
                }
                emitter.onNext(list);
            }
        });
    }
}